package com.machine.filling.function.main.cardWarningFg;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.machine.filling.R;
import com.machine.filling.function.main.base.BaseFgFragment;
import com.machine.filling.function.main.topFg.TopFgFragment;
import com.machine.filling.util.Common;

import java.io.IOException;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

/**
 * Created by Administrator on 2018/1/31 0031.
 * 提示插入油卡
 */

public class CardWarningFgFragment extends BaseFgFragment<CardWarningFgContract.Presenter> implements CardWarningFgContract.View {

    private GifImageView iv_card_insert;
    private TextView tv_back_time;

    @Override
    public View initViews() {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.fg_fg_card, null);
        iv_card_insert = (GifImageView) view.findViewById(R.id.iv_card_insert);
        tv_back_time = (TextView) view.findViewById(R.id.tv_back_time);
        // 如果加载的是gif动图，第一步需要先将gif动图资源转化为GifDrawable
        // 将gif图资源转化为GifDrawable
        GifDrawable gifDrawable = null;
        try {
            gifDrawable = new GifDrawable(getResources(), R.drawable.card_insert);
            iv_card_insert.setImageDrawable(gifDrawable);
        } catch (IOException e) {
            e.printStackTrace();
        }
        iv_card_insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCardInfo();
            }
        });
        ImageView iv_back = (ImageView) view.findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BACK_TIME = 0;
                INextFragment.replaceNext(new Intent(), TopFgFragment.class.getName());
            }
        });

        return view;
    }

    @Override
    public void initDatas() {
        BACK_TIME = Common.BACK_TIME;
        tv_back_time.setText(BACK_TIME + "s");
        handler.sendEmptyMessageDelayed(1001,1*1000);//每隔1秒进行一次发消息更新界面
    }

    private void showCardInfo(){

    }

    private long BACK_TIME = Common.BACK_TIME;//返回主界面的倒计时s
    @SuppressLint("HandlerLeak")
    public Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1001:
                    BACK_TIME--;
                    tv_back_time.setText(BACK_TIME + "s");
                    if (BACK_TIME <= 0){
                        INextFragment.replaceNext(new Intent(), TopFgFragment.class.getName());
                    }else {
                        handler.sendEmptyMessageDelayed(1001,1*1000);
                    }
                    break;
            }
        }
    };;

    @Override
    public void onPause() {
        super.onPause();
        handler.removeMessages(1001);//取消之前所有循环
    }
}
